//
//  AppDelegate.m
//  LiveEducation
//
//  Created by Liu,Qingcan on 2017/4/11.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabViewController.h"


#import "PersistenceCommon.h"
#import "RoomModel.h"
#import "CategoryModel.h"
#import "CategoryAllApi.h"
#import "LECategoryItem.h"


@interface AppDelegate ()

@end

@implementation AppDelegate
//#efb336

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //设置网络请求的总地址
    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
//    config.baseUrl = @"http://localhost:8080";
    config.baseUrl = @"http://192.168.191.4:8080";
//    config.cdnUrl = @"http://fen.bi";

    
    
    //预处理数据
    [self prepareForHomeShow];
    
    
    //测试数据库 (pass)
//    RoomModel *roomModel = [[RoomModel alloc]init];
//    Room *item = [NSEntityDescription insertNewObjectForEntityForName:@"Room" inManagedObjectContext:roomModel.persistence.currentContext];
//
//    item.id = 1;
//    item.feature = @"12";
//    item.validtime = @"15";
//    item.image = @"14";
//    item.title = @"3";
//    item.classhour = @"6";
//    [roomModel insertRoomData:item];
////    [roomModel updateRoom:item];
////    [roomModel deleteRoom:item];
//    [roomModel deleteAllRoom];
//    NSArray *array = [roomModel selectNumberOfData:0];
//    for(Room *r in array){
//        NSLog(@"jkkkkkk00 == %d",r.id);
//        NSLog(@"jkkkkkk11 == %@",r.feature);
//        NSLog(@"jkkkkkk22 == %@",r.validtime);
//        NSLog(@"jkkkkkk33 == %@",r.image);
//        NSLog(@"jkkkkkk44 == %@",r.title);
//        NSLog(@"jkkkkkk55 == %@",r.classhour);
//    }

    
    
    
    MainTabViewController *tab = [[MainTabViewController alloc]init];
//    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:tab];
    
    self.window.rootViewController = tab;
    
    [self.window makeKeyAndVisible];
    
    
    
    
    return YES;
}


- (void)prepareForHomeShow{
//    RoomModel *roomModel = [[RoomModel alloc]init];
    CategoryModel *cateModel = [[CategoryModel alloc]init];
//    Category *item = [NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:cateModel.persistence.currentContext];
        NSArray *array = [cateModel selectAllData];
    if (array.count > 0) {
        return;
    }else{
        //1.发送网络请求，请求数据
        CategoryModel *cateModel = [[CategoryModel alloc]init];
        CategoryAllApi *api = [[CategoryAllApi alloc]init];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            NSDictionary *dict = request.responseJSONObject;
            //2.判断服务器状态码
            NSArray *categories = [NSArray array];
            NSNumber *status = [dict objectForKey:@"statusCode"];
            if (status.integerValue == 1) {
                categories = dict[@"data"];
                //3.success,存数据库
                for(NSDictionary  *dc in categories){
                    LECategoryItem *cate = [LECategoryItem yy_modelWithJSON:dc];
                    [cateModel insertCategoryData:cate];
                }
                 NSLog(@"请求 category分类信息 -- 成功");
            }else{
                //2.输出状态码
                NSLog(@"未能成功获得数据 the reason is : %@",dict[@"statusMsg"]);
            }
            
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            NSLog(@"网络请求 category分类信息失败");
            
            NSError *error = nil;
            [NSException raise:@"网络请求 category分类信息失败" format:@"%@",[error localizedDescription]];
        }];
        
    }
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
